package com.yxq.mdt.app.modules.order.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.orhanobut.hawk.Hawk;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseListFragment;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.dialog.CostumerDialog;
import com.yxq.mdt.app.modules.order.bean.EventOrderMsg;
import com.yxq.mdt.app.modules.order.bean.ExpressBean;
import com.yxq.mdt.app.modules.order.bean.OrderConfirmBean;
import com.yxq.mdt.app.modules.order.bean.OrderDetailBean;
import com.yxq.mdt.app.modules.order.bean.OrderListBean;
import com.yxq.mdt.app.modules.order.bean.OrderPayResultBalanceBean;
import com.yxq.mdt.app.modules.order.bean.OrderPayResultWxBean;
import com.yxq.mdt.app.modules.order.bean.OrderSubmitResult;
import com.yxq.mdt.app.modules.order.model.OrderModel;
import com.yxq.mdt.app.modules.order.prestener.IOrderContract;
import com.yxq.mdt.app.modules.order.prestener.OrderPresenter;
import com.yxq.mdt.app.modules.order.ui.OrderDetailActivity;
import com.yxq.mdt.app.modules.order.ui.OrderRefundDetailActivity;
import com.yxq.mdt.app.modules.order.ui.adapter.OrderAfterSaleListAdapter;
import com.yxq.mdt.app.net.ApiModel;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * @author L
 * @time 2019-11-01
 * @describe 我的订单-售后
 */
public class OrderAfterSaleListFragment extends BaseListFragment<OrderPresenter, OrderListBean.ListBean, BaseViewHolder> implements IOrderContract.View {

    int page = 1;
    List<OrderListBean.ListBean> listBeanList;

    @Override
    public View getEmptyView() {
        return LayoutInflater.from(mContext).inflate(R.layout.layout_empty_order_view, null);
    }

    @Override
    protected BaseQuickAdapter getRecycleViewAdapter() {
        listBeanList = new ArrayList<>();
        mRecycleViewAdapter = new OrderAfterSaleListAdapter(R.layout.layout_order_after_sale_list_item_layout, listBeanList);
        mRecycleViewAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext, OrderRefundDetailActivity.class);
                intent.putExtra("orderId", mRecycleViewAdapter.getItem(position).getOrder_refund_id());
                startActivity(intent);
            }
        });
        mRecycleViewAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                deleteDialog(position,mRecycleViewAdapter.getItem(position));
                return false;
            }
        });
        return mRecycleViewAdapter;
    }
    /**
     * 可删除的订单：待支付、已完成、售后-已处理（处理中不可删除）
     *
     * @param item
     */
    private void deleteDialog(int position,OrderListBean.ListBean item) {
        CostumerDialog dialog = new CostumerDialog.Builder(mContext, R.style.custom_dialog_no_match)
                .setContentView(R.layout.dialog_clear_cache_layout)
                .setCancelable(true)
                .create();

        TextView title = dialog.findViewById(R.id.tv_title);

        title.setText("提示");

        TextView content = dialog.findViewById(R.id.tv_content);
        content.setText("确定删除订单？");

        //继续
        TextView sure = dialog.findViewById(R.id.tv_sure);
        sure.setText("确定");
        sure.setOnClickListener(v -> {
            dialog.dismiss();
            OrderModel.delete(item.getOrder_id(), new ApiModel.CommonCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    ToastUtils.showShort("删除成功");
                    mRecycleViewAdapter.remove(position);
                    EventBus.getDefault().post(new EventOrderMsg(5));
                }

                @Override
                public void onError(String errMsg) {
                    ToastUtils.showShort(errMsg);
                }
            });
        });

        //取消
        TextView cancel = dialog.findViewById(R.id.tv_cancel);
        cancel.setText("取消");
        cancel.setOnClickListener(v -> {
            dialog.dismiss();
        });

        dialog.show();
    }
    @Override
    public void onLoadData(int pageIndex) {
        mPresenter.list("4", pageIndex);
    }

    @Override
    protected OrderPresenter getPresenter() {
        return new OrderPresenter(this, new OrderModel());
    }

    @Override
    public void initData() {
        page = 1;
        onLoadData(page);
        emptyClickListener();
    }

    /**
     * 缺省页按钮点击事件
     */
    private void emptyClickListener() {
        if (mEmptyView != null) {
            mEmptyView.findViewById(R.id.empty_btn_go_shop).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getActivity().setResult(Activity.RESULT_OK);
                    getActivity().finish();
                }
            });
        }
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void onLoadSuccess(OrderConfirmBean beanList) {

    }

    @Override
    public void onLoadFailed(String err) {

    }

    @Override
    public void onLoadListSuccess(OrderListBean orderListBean) {
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
        //是否是隐私模式
        boolean enable = Hawk.get(SPKey.SP_KEY_USER_LOGIN_PRIVACY, false);
        if (enable) {
            mRefreshLayout.finishLoadMore();
            showEmptyLayout();
            return;
        }
        handleListData(orderListBean.getList());
    }

    @Override
    public void onLoadListFailed(String err) {
        ToastUtils.showShort(err);
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
    }

    @Override
    public void onSubmitSuccess(OrderSubmitResult orderSubmitResult) {

    }

    @Override
    public void onSubmitFailed(String err) {

    }

    @Override
    public void onPayBalanceSuccess(OrderPayResultBalanceBean orderPayResultBalanceBean) {

    }

    @Override
    public void onPayBalanceFailed(String err) {

    }

    @Override
    public void onPayWxSuccess(OrderPayResultWxBean orderPayResultWxBean) {

    }

    @Override
    public void onPayWxFailed(String err) {

    }

    @Override
    public void onDetailSuccess(OrderDetailBean listBean) {

    }

    @Override
    public void onDetailFailed(String err) {

    }

    @Override
    public void onExpressSuccess(ExpressBean expressBean) {

    }

    @Override
    public void onExpressFailed(String err) {

    }
}
